//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Cloud.Verifications.Implentations
{
    using System;

    using Microsoft.ServiceBus;
    using Microsoft.ServiceBus.Messaging;

    using Preflight.Model;
    using Preflight.Verifications;

    public abstract class ServiceBusVerificationBase<TDescription> : VerificationBase<TDescription> where TDescription : ServiceBusTopicWriteVerificationDescription
    {
        protected override bool OnBeforeVerification(VerificationResult result)
        {
            return VerificationExtensions.MustSatisfy(
                () => this.Description.GetIssuerName.SuccessfullyYieldsNonNullOrEmptyValue("Issuer Name", result),
                () => this.Description.GetKey.SuccessfullyYieldsNonNullOrEmptyValue("Key", result),
                () => this.Description.GetServiceBusNamespace.SuccessfullyYieldsNonNullOrEmptyValue("Service bus Namespace", result),
                () => this.Description.GetTopicName.SuccessfullyYieldsNonNullOrEmptyValue("Topic name", result));
        }

        protected override void AddVerificationDetail(VerificationResult result)
        {
            result.AddMessage(MessageType.Information, "Service Bus Namespace: {0}", this.Description.GetServiceBusNamespace());
            result.AddMessage(MessageType.Information, "Issuer Name: {0}", this.Description.GetIssuerName());
            result.AddMessage(MessageType.Information, "Key: {0}", this.Description.GetKey());
            result.AddMessage(MessageType.Information, "Topic: {0}", this.Description.GetTopicName());
        }

        protected MessagingFactory CreateMessagingFactory()
        {
            var namespaceUri = ServiceBusEnvironment.CreateServiceUri("sb", this.Description.GetServiceBusNamespace(), string.Empty);
            var tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(this.Description.GetIssuerName(), this.Description.GetKey());
            var settings = new MessagingFactorySettings
            {
                OperationTimeout = TimeSpan.FromMinutes(5),
                TokenProvider = tokenProvider
            };

            return MessagingFactory.Create(namespaceUri, settings);
        }

        protected MessageReceiver CreateMessageReceiver(string path)
        {
            var factory = this.CreateMessagingFactory();
            return factory.CreateMessageReceiver(path, ReceiveMode.PeekLock);
        }
    }
}
